package com.simple.cloud.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.simple.cloud.pojo.Dept;
import com.simple.cloud.service.IDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <h3>springcloud</h3>
 * <p></p>
 *
 * @author : zhengyue
 * @date : 2020-05-19 14:10
 **/
@RestController
@RequestMapping("/dept")
public class DeptController {
    @Autowired
    private IDeptService deptService;
    @Autowired
    private DiscoveryClient client;

    @PostMapping("/add")
    public boolean addDept(Dept dept) {
        dept.setDbSource("cloud01");
        boolean save = deptService.save(dept);
        return save;
    }

    @GetMapping("/get/{id}")
    public Dept get(@PathVariable("id") Long id) {
        Dept dept = deptService.getOne(new QueryWrapper<Dept>().lambda()
            .eq(Dept::getDeptno, id));
        return dept;
    }

    @GetMapping("/get/list")
    public List<Dept> getList() {
        List<Dept> list = deptService.list();
        return list;
    }

    //注册进来的微服务，获取一些消息
    @GetMapping("/discovery")
    public Object discovery() {
        //获取微服务列表的清单
        List<String> services = client.getServices();
        System.out.println("discovery=>services: "+services);

        //等到一个具体的微服务信息
        List<ServiceInstance> instances = client.getInstances("springcloud-provider-dept8001");
        for (ServiceInstance instance : instances) {
            System.out.println(instance.getHost()+"\t"+
                    instance.getPort()+"\t"+
                    instance.getUri()+"\t"+
                    instance.getServiceId());
        }
        return this.client;
    }



}
